import { ApiActionType, EditInfo, User } from '@/types/data'
import { ProfileAction, RootThunkAction } from '@/types/store'
import request from '@/utils/request'
import { Dispatch } from 'react'

export function getUser(): RootThunkAction {
  return async (dispatch:Dispatch<ProfileAction>) => {
    const res = await request.get<ApiActionType<User>>('/user')
    dispatch({
        type:'profile/GET_User',
        payload:res.data.data
    })
  }
}

export function getEdit():RootThunkAction{
 return async(dispatch:Dispatch<ProfileAction>)=>{
     const res = await request.get<ApiActionType<EditInfo>>('/user/profile')
     console.log(res,'edit/res');
     dispatch({
         type:'profile/getUserProfile',
         payload:res.data.data
     })
 }
}

export function updateEdit(key:string,value:string):RootThunkAction{
  return async(dispatch)=>{
     await request.patch('/user/profile', {
      [key]: value
    })
   dispatch( getEdit())
  }
}

export const setFile=(data:FormData):RootThunkAction=>{
 return async(dispatch)=>{
  await request.patch('/user/photo',data)
 dispatch( getEdit())
}
}